package com.example.demo.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.NewResult;
import com.example.demo.common.NullResult;
import com.example.demo.common.Result;
import com.example.demo.entity.Bom;
import com.example.demo.entity.BzxNewPackaging;
import com.example.demo.entity.Packaging;
import com.example.demo.entity.WeightPackaging;
import com.example.demo.mapper.BzxNewPackagingMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api( description  = "包装线（CPS->MES）接口")
@RestController
@RequestMapping("/T_SNWeight")
public class BzxNewPackingController {
    @Resource
    BzxNewPackagingMapper bzxNewPackagingMapper;
    @ApiOperation(value = "开发测试用get", notes="返回数据库信息")
    @GetMapping
    public String findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "40") Integer pageSize,
                           @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<BzxNewPackaging> wrapper = Wrappers.<BzxNewPackaging>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(BzxNewPackaging::getSNNO, search);
        }
        Page<BzxNewPackaging> bomPage = bzxNewPackagingMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return NewResult.success(bomPage);
//        return Result.success(bomMapper.getAll());
    }

    //    3.3	获取产品装箱信息接口（CPS->MES）

    @ApiOperation(value = "获取产品装箱信息接口（CPS->MES）", notes="装箱时根据SN号获取产品装箱信息。")
//    @ApiImplicitParam(name = "telephone", value = "电话号码", paramType = "query", required = true, dataType = "Integer")
    @PostMapping("/GetPackageDataBySN")
    public String GetPackageDataBySN(@RequestBody BzxNewPackaging bzxNewPackaging) {
        QueryWrapper<BzxNewPackaging> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("减速机成品序列号", bzxNewPackaging.getSNNO());
//        Packaging packaging1 = packagingNewMapper.selectOne(queryWrapper);
        System.out.println(bzxNewPackaging.toString());
        List<Map<String, Object>> maps = bzxNewPackagingMapper.selectMaps(queryWrapper);
        Map<String, Object> map = maps.get(0);
        Map<String, Object> newmap = new HashMap<>();
        System.out.println(map.keySet());
        newmap.put("SNNO",map.get("SNNO"));
        newmap.put("SaleOrderNo",map.get("SaleOrderNo"));
        newmap.put("UnitName",map.get("UnitName"));
        newmap.put("NetWeight",map.get("NetWeight"));
        newmap.put("GrossWeight",map.get("GrossWeight"));
        newmap.put("Packing",map.get("Packing"));
        newmap.put("Paint",map.get("Paint"));
        List<Map<String, Object>> listMaps = new ArrayList<Map<String, Object>>();
        listMaps.add(newmap);
//        map.remove("Specification");
//        map.remove("Oil");
//        map.remove("SpeedRatio");
//        map.remove("CheckDate");

//       map.keySet().removeIf(key -> key!= "SNNO");
        return NewResult.success(listMaps);
//        if (maps.size() == 0){
//            if (packaging.get写入请求次数() == null){
//                packaging.set写入请求次数(1);
//            }else{
//                packaging.set写入请求次数(packaging.get写入请求次数()+1);
//            }
//            packagingNewMapper.insert(packaging);
//        }else{
////            int i = (int) maps.get(0).get("写入请求次数");
//            packaging.set写入请求次数((int) maps.get(0).get("写入请求次数")+1);
//            packagingNewMapper.update(packaging,queryWrapper);
//        }
//        packaging.set写入请求次数((int) maps.get(0).get("写入请求次数")+1);
//            packagingNewMapper.update(packaging,queryWrapper);
//        System.out.println((int) maps.get(0).get("写入请求次数"));
//        if (packaging.get写入请求次数() == null){
//            packaging.set写入请求次数(1);
//        }else{
//            packaging.set写入请求次数(packaging.get写入请求次数()+1);
//        }
//        packagingNewMapper.insert(packaging);
//        return Result.success();
    }

    //3.4	获取激光打标信息接口（CPS->MES）
    @ApiOperation(value = "获取激光打标信息接口（CPS->MES）", notes="激光打标时根据SN号获取产品打标信息。")
    @PostMapping("/GetLaserMarkDataBySN")
    public String GetLaserMarkDataBySN(@RequestBody BzxNewPackaging bzxNewPackaging) {
        QueryWrapper<BzxNewPackaging> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("减速机成品序列号", bzxNewPackaging.getSNNO());
//        Packaging packaging1 = packagingNewMapper.selectOne(queryWrapper);
        System.out.println(bzxNewPackaging.toString());
        List<Map<String, Object>> maps = bzxNewPackagingMapper.selectMaps(queryWrapper);
        Map<String, Object> map = maps.get(0);
        System.out.println(map.keySet());
        Map<String, Object> newmap = new HashMap<>();
        newmap.put("SNNO",map.get("SNNO"));
        newmap.put("Specification",map.get("Specification"));
        newmap.put("SpeedRatio",map.get("SpeedRatio"));
        newmap.put("NetWeight",map.get("NetWeight"));
        newmap.put("GrossWeight",map.get("GrossWeight"));
        newmap.put("UnitName",map.get("UnitName"));
        newmap.put("Oil",map.get("Oil"));
        newmap.put("Size",map.get("铭牌大小"));
        List<Map<String, Object>> listMaps = new ArrayList<Map<String, Object>>();
        listMaps.add(newmap);

        return NewResult.success(listMaps);
    }

    //    3.5	获取合格证信息接口（CPS->MES）
    @ApiOperation(value = "获取合格证信息接口（CPS->MES）", notes="打印合格证时根据SN号获取合格证信息。")
    @PostMapping("/GetCertificateDataBySN")
    public String GetCertificateDataBySN(@RequestBody BzxNewPackaging bzxNewPackaging) {
        QueryWrapper<BzxNewPackaging> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("减速机成品序列号", bzxNewPackaging.getSNNO());
        List<Map<String, Object>> maps = bzxNewPackagingMapper.selectMaps(queryWrapper);
        Map<String, Object> map = maps.get(0);
        List<Map<String, Object>> listMaps = new ArrayList<Map<String, Object>>();
        Map<String, Object> newmap = new HashMap<String, Object>();
        newmap.put("SNNO", map.get("SNNO"));
        newmap.put("Specification", map.get("Specification"));
        newmap.put("CheckDate", DateUtil.today());
        listMaps.add(newmap);
        return NewResult.success(listMaps);
    }

    //    3.2	上传称重数据接口（CPS->MES）
    @ApiOperation(value = "上传称重数据接口（CPS->MES）", notes="包装线称重时传递重量数据。")
    @PostMapping("/SaveWeightData")
    public NullResult SaveWeightData(@RequestBody List<WeightPackaging> weightPackagings) {
//        WeightPackaging weightPackaging = new WeightPackaging();
        for (WeightPackaging weightPackaging : weightPackagings){
//            System.out.println(weightPackaging.toString());
            BzxNewPackaging bzxNewPackaging = new BzxNewPackaging();
            QueryWrapper<BzxNewPackaging> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("减速机成品序列号", weightPackaging.getSnno());
            List<Map<String, Object>> maps = bzxNewPackagingMapper.selectMaps(queryWrapper);
            Map<String, Object> map = maps.get(0);
            if (weightPackaging.getWeightType()==0){
                System.out.println(weightPackaging.getWeight());
                bzxNewPackaging.setNetWeight(weightPackaging.getWeight());
                bzxNewPackaging.setMachineId(weightPackaging.getMachineId());
                bzxNewPackaging.setIsWeight(1);
                bzxNewPackagingMapper.update(bzxNewPackaging,queryWrapper);
            }else if (weightPackaging.getWeightType()==1){
                bzxNewPackaging.setGrossWeight(weightPackaging.getWeight());
                bzxNewPackaging.setMachineId(weightPackaging.getMachineId());
                bzxNewPackagingMapper.update(bzxNewPackaging,queryWrapper);
                bzxNewPackaging.setIsWeight(1);
            }
        }

//        QueryWrapper<BzxNewPackaging> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("减速机成品序列号", bzxNewPackaging.getSNNO());
//        List<Map<String, Object>> maps = bzxNewPackagingMapper.selectMaps(queryWrapper);
//        Map<String, Object> map = maps.get(0);
//        List<Map<String, Object>> listMaps = new ArrayList<Map<String, Object>>();
//        Map<String, Object> newmap = new HashMap<String, Object>();
//        newmap.put("SNNO", map.get("SNNO"));
//        newmap.put("Specification", map.get("Specification"));
//        newmap.put("CheckDate", DateUtil.today());
//        listMaps.add(newmap);
        return NullResult.success();
    }

}
